package com.zjtaixue.usersite.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zjtaixue.usersite.domain.Login;
import com.zjtaixue.usersite.mapper.LoginMapper;
import com.zjtaixue.usersite.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public JSONObject login(Login login) {
        JSONObject jsonResult = new JSONObject();
        try {
            Login byUsername = loginMapper.getByUsername(login.getUsername());
            if (byUsername == null) {
                jsonResult.put("res", 2);
            } else {
                if (!byUsername.getPassword().equals(login.getPassword())) {
                    jsonResult.put("res", 3);
                } else if (byUsername.getState() == 0) {
                    jsonResult.put("res", 4);
                } else if (!byUsername.getType().equals(login.getType())) {
                    jsonResult.put("res", 5);
                } else {
                    jsonResult.put("res", 1);
                    jsonResult.put("data", byUsername);
                }
            }
        } catch (Exception e) {
            jsonResult.put("res", 0);
        }
        return jsonResult;
    }

    @Override
    public Login getByUsername(String username) {
        return loginMapper.getByUsername(username);
    }

    @Override
    public Login teacherLogin(Login login) {
        return loginMapper.teacherLogin(login);
    }

    @Override
    public Login studentLogin(Login login) {
        log.info("学生的登入信息是:"+login);
        Login studentLogin=loginMapper.studentLogin(login);
        log.info("返回的学生的登入信息是:"+studentLogin);
        return loginMapper.studentLogin(login);
    }

    @Override
    public int enable(Integer[] loginIds) {
        int count = 0;
        for (Integer loginId : loginIds) {
            count += loginMapper.enable(loginId);
        }
        return count;
    }

    @Override
    public int disable(Integer[] loginIds) {
        int count = 0;
        for (Integer loginId : loginIds) {
            count += loginMapper.disable(loginId);
        }
        return count;
    }

    @Override
    public int update(Login login) {
        return loginMapper.updateByPrimaryKey(login);
    }

    @Override
    public Login getById(Integer id) {
        return loginMapper.selectByPrimaryKey(id);
    }
}
